<!doctype html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>确认付款</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="../css/mui.min.css" rel="stylesheet" />
		<link rel="stylesheet" href="../css/style.css" />
		<link rel="stylesheet" href="../css/iconfont.css" />
		<link rel="stylesheet" href="../css/wallet.css" />
	</head>

	<body>
		<div class="mui-content qrfuk_content">
			<div class="fuk_conbox">
				<div class="fuk_ctop">
					<div class="fuk_ctimg"><img id='store_thum'></div>
					<div class="fuk_spnam"></div>
				</div>
				<div class="fuk_center">
					<div>消费金额</div>
					<div class="fuk_pribox">
						<label>¥</label>
						<input id='pay_money' style="text-align: left;" onkeyup="clearNoNum(this)" type="text"></input>
					</div>
					<!--<div class="fuk_jif">
	    			<p>您本次可以使用鼓励金抵消费额度</p>
	    			<p class="fuk_jifnum"><em>80</em>鼓励金</p>
	    		</div>-->
				</div>
				<!--<p class="liuy">
					<a href="#">添加留言</a>
				</p>-->
				<button type="button" class="qrfuk_btn" id='qrfuk_btn'>确认付款</button>
			</div>
			<div class="fuk_sm" style="position: relative; margin-top: 80px;">©由龙信天下提供技术支持</div>
		</div>
		<!----------------悬浮框  设置支付密码 ------------------->
		<div class="mask">
			<div class="mask_con">
				<div class="top">
					<span class="del"><button type="button">X</button></span>
					<span class="tx"><img src="../images/cpimg.jpg"></span>
					<span class="wz">请输入支付密码</span>
				</div>
				<div class="xf_pribox">
					<p class="xf_fknam">向“<em>爱上漂亮美业</em>”付款</p>
					<p class="xf_pri">¥<em>100</em></p>
				</div>
				<div class="xffkfs">
					<a href="#"><span class="img"><img src="../images/jinbi.gif"></span>使用鼓励金支付</a>
				</div>
				
			</div>
		</div>
		<!----------------悬浮框  填写支付密码------------------->
		<div class="mask" id='pay_by_score' >
			<div class="mask_con">
				<div class="top">
					<span class="del"><button type="button" onclick="$('#pay_by_score').hide();$('#qrfuk_btn').attr('disabled', false);">X</button></span>
					<span class="tx"><img src="../images/cpimg.jpg"></span>
					<span class="wz">请输入支付密码<a href="pay_pwd.html" style="float: right;font-size: 14px;color: red;">忘记密码</a></span>
				</div>
				<div class="xf_pribox">
					<p class="xf_fknam">向“<em id='jf_store_name'>爱上漂亮美业</em>”付款</p>
					<p class="xf_pri">¥<em id='jf_pay_money'>100</em></p>
				</div>
				<div class="xffkfs">
					<a href="#" id='goto_shop'><span class="img"><img src="../images/jinbi.gif">
				</span>该商家支持鼓励金100%抵扣付款</a>
				</div>
				<div class="xfk_btn" id='is_pay_pwd' style="display: none;"><button type="button">设置支付密码</button></div>
				<div class="pwd-box" id='pay_pwd' style="none">  
				    <input type="tel" maxlength="6" class="pwd-input" id="pwd-input">  
				    <div class="fake-box">  
				        <input type="password" readonly="">  
				        <input type="password" readonly="">  
				        <input type="password" readonly="">  
				        <input type="password" readonly="">  
				        <input type="password" readonly="">  
				        <input type="password" readonly="">  
				    </div>  
				</div> 
			</div>
			<div id="callback"></div>
			<div id="cut_back"></div>
			<input type="hidden" id='is_pwd' value=1><!--是否设置支付密码-->
		</div>
		<input type="hidden" id='order_sn'>
		<input type="hidden" id='give_score'>
	</body>

</html>
<script src="../js/mui.min.js"></script>
<script src="../js/zepto.min.js"></script>
<script type="text/javascript" src="../js/date.js"></script>

<script type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript" src="../js/layer/layer.min.js"></script>
<script type="text/javascript" src="../js/app.js"></script>
<script>
	var chain_id=getUrlParam("chain_id");//分店id
	if(!chain_id){
		chain_id="";
	}
	$(function() {
		//记录扫码客户端，微信，或支付宝
		var pay_type = 0;
		var ua = window.navigator.userAgent.toLowerCase();
		var auth_code = getQueryString("auth_code");
		if(ua.match(/MicroMessenger/i) == 'micromessenger') {
			pay_type = 2;
		} else if(ua.match(/AlipayClient/i) == 'alipayclient') {
			pay_type = 3;
			if(auth_code == null) {
				var fromurl = location.href;
				var url = 'https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=2016030801195057&scope=auth_base&redirect_uri='+encodeURIComponent(fromurl);
				location.href = url;
			}
		}
		
		//获取商户信息
		$.ajax({
			async: false, 
			url: APP.api_url + 'wallet/get_storeby_id',
			data: {
				store_id: getUrlParam("store_id"),
			},
			success: function(data) {
				if(data.code == 200) {
					localStorage.setItem("cut_store_info", JSON.stringify(data.data));
					$('.fuk_spnam').text(data.data.store_name);
		
					//图片地址
					var picture_url = user_tupian(data.data.store_logo,'../images/cpimg.jpg');
					$('#store_thum').attr('src',picture_url);
					
					
				} else {
					layer.msg('该店铺不存在');
					return false;
				}
			}
		})
		
		//确认付款
		$("#qrfuk_btn").click(function() {
			$("#qrfuk_btn").attr("disabled", true);
			//付款金额
			var pay_money = $("#pay_money").val();
			if(pay_money == "") {
					layer.msg('请输入支付金额');
					$("#qrfuk_btn").attr("disabled", false);
					return false;
			}
			//获取店铺信息
			var store_info = JSON.parse(localStorage.getItem("cut_store_info"));
			
			if(store_info['industry_id'] == 7){
				addcookie("navigation", "mz",1);
			}
			
			if(pay_type == 3){
				//获取支付宝key
				alipay_user(auth_code,getUrlParam("fromuid"));
			}else if(pay_type == 2){
				//获取openid
				var openid = localStorage.getItem('openid');
				//获取用户信息
				member_by_openid(openid,store_info['u_id']);
			}
			var member = JSON.parse(localStorage.getItem('member'));
			
			//是否单店铺
			if(store_info['is_round'] == 0){
				//获取用户在当前店铺鼓励金使用情况
				apply_score_user(member['id'],store_info['id'],'give_score');
			}
			
			//订单编号
			$('#order_sn').val(getSn());
			//店铺鼓励金比例
			var bili = store_info['ad_score'];
			//所需鼓励金
			var need_score = parseFloat((pay_money * 100 * bili) / 10000);
			
			
			//检查是否单店铺
			if(store_info['is_round'] == 0 && bili != 100 && parseFloat($("#give_score").val())>=parseFloat(need_score) ){//组合付款
				union_pay(need_score,pay_money,pay_type);
			}else if(store_info['is_round'] == 0 && bili != 100 && parseFloat($("#give_score").val())<parseFloat(need_score) ){ //全额付款
				union_pay(pay_money,pay_money,pay_type);
			}else if(store_info['is_round'] == 0 && bili == 100 && parseFloat($("#give_score").val())>=parseFloat(need_score) ){//纯鼓励金付款
				all_score_pay(pay_money);
			}else if(store_info['is_round'] == 0 && bili == 100 && parseFloat($("#give_score").val())<parseFloat(need_score) ){//全额付款
				union_pay(pay_money,pay_money,pay_type);
			}else if(store_info['is_round'] != 0 && bili != 100 && parseFloat(member['credit1'])>=parseFloat(need_score) ){//组合付款
				union_pay(need_score,pay_money,pay_type);
			}else if(store_info['is_round'] != 0 && bili != 100 && parseFloat(member['credit1'])<parseFloat(need_score) ){//全额付款
				union_pay(pay_money,pay_money,pay_type);
			}else if(store_info['is_round'] != 0 && bili == 100 && parseFloat(member['credit1'])>=parseFloat(need_score) ){//纯鼓励金付款
				all_score_pay(pay_money);
			}else if(store_info['is_round'] != 0 && bili == 100 && parseFloat(member['credit1'])<parseFloat(need_score) ){//全额付款
				union_pay(pay_money,pay_money,pay_type);
			}

		});
		
		//店铺链接
		$('#store_thum').click(function(){
			var store_info = JSON.parse(localStorage.getItem("cut_store_info"));
			
			var index = "shop.html?";
			if(store_info.template_id &&  store_info.template_id!="") {
				index = "shop" + store_info.template_id + ".html?";
			}
			location.href = APP.web_root+index+'store_id='+store_info['id']+'&fromuid='+getUrlParam("store_id");
		});
	});



//組合付款
function union_pay(need_score,pay_money,pay_type){
	var member = JSON.parse(localStorage.getItem('member'));
	//获取店铺信息
	var store_info = JSON.parse(localStorage.getItem("cut_store_info"));
	var act = "alipay";
	var jstype = "html";
	var cut_pay_money = pay_money;
	if(member['credit1'] !=null && member['credit1']> need_score && pay_money>=1 && store_info['is_union_score'] ==1) {
		if(parseFloat(need_score) != parseFloat(pay_money)){
			//实付金额
			cut_pay_money = (parseFloat((pay_money*100) -(need_score*100))/100).toFixed(2);
		}
	}
				
	if(parseFloat(pay_money) == parseFloat(cut_pay_money) && parseFloat(store_info['score']) <parseFloat(cut_pay_money) && store_info['is_union_score'] ==1){
		//判断商户鼓励金是否充足
		layer.msg('店铺鼓励金不足，无法赠送鼓励金');
		$("#qrfuk_btn").attr("disabled", false);
		return false;
	}
	
	if(pay_type == 2) {
		act = "wechat";
		jstype = "jsonp";
	}
			
	$.ajax({
		url: APP.api_url + 'Payment/' + act,
		data: {
				WIDout_trade_no	: $("#order_sn").val(),
				WIDsubject		: "二维码付款",
				WIDtotal_fee	: cut_pay_money,
				//WIDtotal_fee	: 0.01,
				WIDshow_url		: 'www',
				WIDbody			: "二维码付款",
				uid				: getUid(),
				order_ids		: store_info['id'],
				type			: "ads_order",
				user_type		: 1,
				pay_money		: pay_money,
				token			: get_token(),
				openid			: member['openid'],
				pay_type		: pay_type,
				chain_id		: chain_id
			},
		type: "post",
		dataType: jstype,
		jsonp: 'callback',
		jsonpCallback: 'jsonpCallback',
		success: function(data) {
			if(pay_type == 2) {
				callpay(data);
			} else {
				$("#callback").append(data);
			}
		}
	})
}





//純積分付款
function all_score_pay(pay_money){
	var member = JSON.parse(localStorage.getItem('member'));
	//获取店铺信息
	var store_info = JSON.parse(localStorage.getItem("cut_store_info"));
	$("#pay_by_score").show();
	if(member['pay_password'] == null || member['pay_password'].length <1){
		$("#is_pay_pwd").show();
		$("#is_pwd").val(2);
	}
				
		//商家名称
		if(store_info['store_name'] !=null && store_info['store_name'] !=""){
			$("#jf_store_name").text(store_info['store_name']); 
		}else{
			$("#jf_store_name").text("商家"); 
		}
		//付款金额
		$("#jf_pay_money").text(pay_money); 
				
		var $input = $(".fake-box input");  
	    $("#pwd-input").on("input", function() {  
	          	var pwd = $(this).val().trim();  
	           	for (var i = 0, len = pwd.length; i < len; i++) {  
	                $input.eq("" + i + "").val(pwd[i]);  
	            }  
	            $input.each(function() {  
	                var index = $(this).index();  
	                if (index >= len) {  
	                    $(this).val("");  
	                }
	           	});  
	            if (len == 6) {  
	             		//鼓励金支付
	             	$.ajax({
	             			async: false, 
							url: APP.api_url + 'Payment/pay_by_score',
							data: {
								WIDout_trade_no	: $("#order_sn").val(),		
								is_pwd			: $("#is_pwd").val(),
								title			: '二维码付款',
								WIDtotal_fee	: $("#pay_money").val(),
								uid				: getUid(),
								token			: get_token(),
								pay_password	: pwd,
								order_ids		: store_info['id'],		//店铺id
								type			: 'score_pay',
								user_type		: 1,
								pay_type		: 5,
								chain_id		:chain_id
							},
							type: "post",
							dataType:"json",
							success: function(data) {
								if(data.code == 200){
									//更新用户信息
									localStorage.setItem('member', JSON.stringify(data.data));
									location.href="fuk-success.html?sn="+$("#order_sn").val();
								}else{
									layer.msg(data.msg);
								}
								
							}
						})
	                }
	            });  
}
	
	
	
	
//只能数字和小数点
	function clearNoNum(obj) {
		obj.value = obj.value.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
		obj.value = obj.value.replace(/^\./g, ""); //验证第一个字符是数字而不是.
		obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的.
		obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
	}
	//调用微信JS api 支付
	function jsApiCall(jsApiParameters) {
		WeixinJSBridge.invoke('getBrandWCPayRequest', jsApiParameters,
			function(res) {
				if(res.err_msg == "get_brand_wcpay_request:ok") {
					var store_info = JSON.parse(sessionStorage.getItem("cut_store_info"));
					layer.msg('付款成功！', {
						  time: 2000
						}, function(){
							location.href="fuk-success.html?sn="+$("#order_sn").val();
						});
				} else {
					layer.msg('付款失败');
					$("#qrfuk_btn").attr("disabled",false);
				}
			});
	}

	function callpay(jsApiParameters) {
		console.log(jsApiParameters);
		if(typeof WeixinJSBridge == "undefined") {
			if(document.addEventListener) {
				document.addEventListener('WeixinJSBridgeReady', jsApiCall(jsApiParameters), false);
			} else if(document.attachEvent) {
				document.attachEvent('WeixinJSBridgeReady', jsApiCall(jsApiParameters));
				document.attachEvent('onWeixinJSBridgeReady', jsApiCall(jsApiParameters));
			}
		} else {
			jsApiCall(jsApiParameters);
		}
	}
	function jsonpCallback(data) {
		$("#cut_back").append(data);
	}

	//获取用户在店铺鼓励金使用情况
	function apply_score_user(user_id,store_id,ement_id){
		$.ajax({
			//type: 'post',
			async: false, 
			url: APP.api_url + 'wallet/apply_score_user',
			data: {
				user_id		: user_id,
				store_id	: store_id,
				token 		:'cheshi'
			},
			success: function(data) {
				if(data.code == 200) {
					$("#"+ement_id).val(data.data);
				} else {
					layer.msg(data.msg);
				}
			}
		});
	}
</script>
